package com.terabits.smartriver.service.impl;

import com.terabits.smartriver.constants.AuthorityEnum;
import com.terabits.smartriver.mapper.RoleMapper;
import com.terabits.smartriver.meta.po.Role;
import com.terabits.smartriver.meta.vo.MenuVO;
import com.terabits.smartriver.meta.vo.RoleMenuVO;
import com.terabits.smartriver.meta.vo.UserRoleVO;
import com.terabits.smartriver.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Yin on 2019/3/6
 */
@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class AuthorityServiceImpl extends BaseService implements AuthorityService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Map> getMenuByUser(Integer type, Integer userId) {
        return roleMapper.selectMenuByUser(type, userId);
    }

    @Override
    public String getUserProjectNum(Integer userId) {
        return roleMapper.getUserProjectNum(userId);

    }

    @Override
    public List<Role> getRole() {
        return roleMapper.selectAll();
    }

    @Override
    public List<Role> getRoleByUser(String username) {
        return roleMapper.selectRoleByUser(username);
    }

    @Override
    public int addRole(Role role) {
        role.setValue(role.getValue().toUpperCase());
        List roles = findObjectsByProperty("value", role.getValue(), roleMapper, Role.class);

        if (roles != null && roles.size() != 0) {
            return -1;
        }

        return roleMapper.insertSelective(role);
    }

    @Override
    public int deleteRole(Integer roleId) {
        //先删除两个关联表
        roleMapper.deleteUserRoleByRoleId(roleId);
        roleMapper.deleteRoleMenu(AuthorityEnum.MENU_TYPE_WX.getType(), roleId);
        roleMapper.deleteRoleMenu(AuthorityEnum.MENU_TYPE_PC.getType(), roleId);
        //在删除角色
        return roleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public List<UserRoleVO> getUserRole() {
        return roleMapper.selectUserRole();
    }

    @Override
    public int updateUserRole(Integer userId, List<Integer> roleIds) {
        int res;
        //先删除原有对应关系
        res = roleMapper.deleteUserRole(userId);
        //添加对应关系
        res = res * roleMapper.insertUserRole(userId, roleIds);

        return res;
    }

    @Override
    public List<MenuVO> getMenu(Integer type, Integer roleId) {
        if (StringUtils.isEmpty(roleId)) {
            return roleMapper.selectMenu(type);
        }
        return roleMapper.selectMenuByRole(type, roleId);
    }

    @Override
    public List<RoleMenuVO> getRoleMenu(Integer type) {
        return roleMapper.selectRoleMenu(type);
    }

    @Override
    public int updateRoleMenu(Integer type, Integer roleId, List<Integer> menuIds) {

        //先删除原有对应关系 删除是类型type
        roleMapper.deleteRoleMenu(type, roleId);

        StringBuffer menuIdsStr = new StringBuffer();
        for (Integer menuId : menuIds) {
            menuIdsStr.append(",").append(menuId);
        }
        //剔除ismenu=1的
        List<Integer> isMenuIds = roleMapper.selectMenuByIsMenu(menuIdsStr.substring(1));

        return roleMapper.insertRoleMenu(roleId, isMenuIds);
    }


}
